package com.aponteitor.desktop.event;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.TimerTask;

import com.aponteitor.desktop.Program;
import com.aponteitor.desktop.ui.MessagePanel;

/**
 * @author wellsantos@wellsantos.com
 */
public class HideMessageHandler implements MouseListener, KeyListener, ActionListener, Runnable {

    private static final HideMessageHandler instance = new HideMessageHandler();
    
    private int messageShowCount;

    public static HideMessageHandler get() {
        return instance;
    }
    
    public void reset(){
        TimerTask task = new TimerTask() {            
            @Override
            public void run() {
                messageShowCount = MessagePanel.getMessageShowCount();
            }
        };
        Program.getTimer().schedule(task, 500);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        EventQueue.invokeLater(this);
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        EventQueue.invokeLater(this);
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        EventQueue.invokeLater(this);
    }

    @Override
    public void run() {
        if (messageShowCount == MessagePanel.getMessageShowCount()){
            MessagePanel messagePanel = Program.getActiveMessagePanel();
            if (messagePanel != null) {
                messagePanel.setVisible(false);
            }
        }
        messageShowCount = MessagePanel.getMessageShowCount();
    }

}
